#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;

class info{
public:
    info(){}
    info(int acc,int maxValue,int minValue){
        this->isAcc = acc;
        this->maxValue = maxValue;
        this->minValue = minValue;
    }
public:
    bool isAcc;
    int maxValue;
    int minValue;
};

class Solution {
public:
    bool verifyPostorder(vector<int>& postorder) {
        if(postorder.empty()){
            return true;
        }
        return func(postorder).isAcc;
    }
    info func(vector<int> postorder){
     
        if(postorder.size()==1){
            return info(true,postorder[0],postorder[0]);
        }
        int tempVAule = *postorder.rbegin();
        int a = 1;
        postorder.pop_back();
        auto index = find_if(postorder.begin(),postorder.end(),[tempVAule](const int& a){return a>tempVAule;});
        vector<int> leftArr(postorder.begin(),index);
        vector<int> rightArr(index,postorder.end());
        info infoLeft;
        info infoRight ;
        if(!leftArr.empty()){
            infoLeft = func(leftArr);
        }
        else{
            infoLeft = info(true,tempVAule,tempVAule);
        }
        if(!rightArr.empty()){
            infoRight = func(rightArr);
        }
        else{
            infoRight = info(true,tempVAule,tempVAule);
        }
        if(infoLeft.isAcc&&infoRight.isAcc){
            if(tempVAule>=infoLeft.maxValue&&tempVAule<=infoRight.minValue){
                return info(true,infoRight.maxValue,infoLeft.minValue);
            }
        }
        else{
            return info(false,0,0);
        }
        return info(false,0,0);
    }

};

int main(){
    Solution S;
    vector<int> N = {1,3,2,4};
    cout<<S.verifyPostorder(N)<<endl;
}